// 预定义
var seljob = "";
var departments;// 部门列表
var jobs;// 职位列表

//页面载入初始化
$( "body" ).ready( function( e )
{
	//init();

	if( $( "#department" ).length > 0 )
	{
		$( "#department" ).bind( "change", departmentChanged );
		initSelectJob( seljob, "department", "job" );
	}

	select_date( 'birthday' );
	select_date( 'entryday' );

	$( "body" ).bind( "keyup", function( e )
	{
		if( e.which == 13 )
			submit();
	} );

	$( "#__SEL_AVATAR" ).bind( "dblclick", function()
	{
		$( this ).hide();
	} );

	$( "#__SEL_AVATAR" ).bind( "click", function( e )
	{
		e.stopPropagation();
	} );

	$( "#__SEL_AVATAR img" ).hover( function()
	{
		$( this ).css( "border-color", "#ffcc00" );
	}, function()
	{
		$( this ).css( "border-color", "#f0f0f0" );
	} );

	$( "#__SEL_AVATAR img" ).bind( "click", function()
	{
		var p = $( "#photo" );
		var src = $( this ).attr( "src" );
		p.attr( "src", src );

		var sa = src.split( /\/|\\/ );
		avatar = sa[ sa.length - 1 ];
		$( "#__SEL_AVATAR" ).hide();
	} );

	$( document ).bind( "click", function( e )
	{
		$( "#__SEL_AVATAR" ).hide();
	} );

	$( "#photo" ).bind( "click", function( e )
	{
		// 阻止向上冒泡，从而禁止触发 body 点击事件
		e.stopPropagation();

		var ps = $( "#__SEL_AVATAR" );

		if( "none" != ps.css( "display" ) )
			return;

		var p = $( this );
		var pos = p.offset();

		ps.show();
		ps.css( { left: pos.left - 5, top: pos.top + p.height() + 5 } );
	} );
} );

function select_date( targetid )
{
	$( '#' + targetid ).Zebra_DatePicker( { format: 'Y-m-d' } );
}

// 初始化选定当前对象的职位
function initSelectJob( key, departmentid, jobid )
{
	var par;

	if( key != "" )
	{
		var job = jobs[ key ];
		par = job.parent;

		// 部门选中
		$( "#" + departmentid + " option[value=" + job.parent + "]" ).attr( "selected", true );
	}
	else
	{
		par = "jszb";
	}

	// 动态创建职位列表
	updateJobSelect( jobid, par, key );
}

function departmentChanged()
{
	var key = $( this ).val();
	updateJobSelect( "job", key, seljob );
}

function updateJobSelect( jobid, pid, key )
{
	var j = $( "#" + jobid );

	// 动态创建职位列表，每次创建前先清空历史条目
	j.empty();
	for( k in jobs )
	{
		if( jobs[ k ].parent == pid )
		{
			j.append( "<option value='" + k + "'" + ( k == key ? "selected='selected'" : "" ) + ">"
					+ jobs[ k ].name + "</option>" );
		}
	}
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------
